<!DOCTYPE html><html><head>
      <title>YOLOX&#x8BAD;&#x7EC3;&#x548C;&#x8C03;&#x7528;&#x6307;&#x5357;</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      
      <link rel="stylesheet" href="file:////home/dworry/.vscode/extensions/shd101wyy.markdown-preview-enhanced-0.6.8/node_modules/@shd101wyy/mume/dependencies/katex/katex.min.css">
      
      
      
      
      
      
      
      
      
      <style>
      /**
 * prism.js Github theme based on GitHub's theme.
 * @author Sam Clarke
 */
code[class*="language-"],
pre[class*="language-"] {
  color: #333;
  background: none;
  font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
  text-align: left;
  white-space: pre;
  word-spacing: normal;
  word-break: normal;
  word-wrap: normal;
  line-height: 1.4;

  -moz-tab-size: 8;
  -o-tab-size: 8;
  tab-size: 8;

  -webkit-hyphens: none;
  -moz-hyphens: none;
  -ms-hyphens: none;
  hyphens: none;
}

/* Code blocks */
pre[class*="language-"] {
  padding: .8em;
  overflow: auto;
  /* border: 1px solid #ddd; */
  border-radius: 3px;
  /* background: #fff; */
  background: #f5f5f5;
}

/* Inline code */
:not(pre) > code[class*="language-"] {
  padding: .1em;
  border-radius: .3em;
  white-space: normal;
  background: #f5f5f5;
}

.token.comment,
.token.blockquote {
  color: #969896;
}

.token.cdata {
  color: #183691;
}

.token.doctype,
.token.punctuation,
.token.variable,
.token.macro.property {
  color: #333;
}

.token.operator,
.token.important,
.token.keyword,
.token.rule,
.token.builtin {
  color: #a71d5d;
}

.token.string,
.token.url,
.token.regex,
.token.attr-value {
  color: #183691;
}

.token.property,
.token.number,
.token.boolean,
.token.entity,
.token.atrule,
.token.constant,
.token.symbol,
.token.command,
.token.code {
  color: #0086b3;
}

.token.tag,
.token.selector,
.token.prolog {
  color: #63a35c;
}

.token.function,
.token.namespace,
.token.pseudo-element,
.token.class,
.token.class-name,
.token.pseudo-class,
.token.id,
.token.url-reference .token.variable,
.token.attr-name {
  color: #795da3;
}

.token.entity {
  cursor: help;
}

.token.title,
.token.title .token.punctuation {
  font-weight: bold;
  color: #1d3e81;
}

.token.list {
  color: #ed6a43;
}

.token.inserted {
  background-color: #eaffea;
  color: #55a532;
}

.token.deleted {
  background-color: #ffecec;
  color: #bd2c00;
}

.token.bold {
  font-weight: bold;
}

.token.italic {
  font-style: italic;
}


/* JSON */
.language-json .token.property {
  color: #183691;
}

.language-markup .token.tag .token.punctuation {
  color: #333;
}

/* CSS */
code.language-css,
.language-css .token.function {
  color: #0086b3;
}

/* YAML */
.language-yaml .token.atrule {
  color: #63a35c;
}

code.language-yaml {
  color: #183691;
}

/* Ruby */
.language-ruby .token.function {
  color: #333;
}

/* Markdown */
.language-markdown .token.url {
  color: #795da3;
}

/* Makefile */
.language-makefile .token.symbol {
  color: #795da3;
}

.language-makefile .token.variable {
  color: #183691;
}

.language-makefile .token.builtin {
  color: #0086b3;
}

/* Bash */
.language-bash .token.keyword {
  color: #0086b3;
}

/* highlight */
pre[data-line] {
  position: relative;
  padding: 1em 0 1em 3em;
}
pre[data-line] .line-highlight-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  background-color: transparent;
  display: block;
  width: 100%;
}

pre[data-line] .line-highlight {
  position: absolute;
  left: 0;
  right: 0;
  padding: inherit 0;
  margin-top: 1em;
  background: hsla(24, 20%, 50%,.08);
  background: linear-gradient(to right, hsla(24, 20%, 50%,.1) 70%, hsla(24, 20%, 50%,0));
  pointer-events: none;
  line-height: inherit;
  white-space: pre;
}

pre[data-line] .line-highlight:before, 
pre[data-line] .line-highlight[data-end]:after {
  content: attr(data-start);
  position: absolute;
  top: .4em;
  left: .6em;
  min-width: 1em;
  padding: 0 .5em;
  background-color: hsla(24, 20%, 50%,.4);
  color: hsl(24, 20%, 95%);
  font: bold 65%/1.5 sans-serif;
  text-align: center;
  vertical-align: .3em;
  border-radius: 999px;
  text-shadow: none;
  box-shadow: 0 1px white;
}

pre[data-line] .line-highlight[data-end]:after {
  content: attr(data-end);
  top: auto;
  bottom: .4em;
}html body{font-family:"Helvetica Neue",Helvetica,"Segoe UI",Arial,freesans,sans-serif;font-size:16px;line-height:1.6;color:#333;background-color:#fff;overflow:initial;box-sizing:border-box;word-wrap:break-word}html body>:first-child{margin-top:0}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{line-height:1.2;margin-top:1em;margin-bottom:16px;color:#000}html body h1{font-size:2.25em;font-weight:300;padding-bottom:.3em}html body h2{font-size:1.75em;font-weight:400;padding-bottom:.3em}html body h3{font-size:1.5em;font-weight:500}html body h4{font-size:1.25em;font-weight:600}html body h5{font-size:1.1em;font-weight:600}html body h6{font-size:1em;font-weight:600}html body h1,html body h2,html body h3,html body h4,html body h5{font-weight:600}html body h5{font-size:1em}html body h6{color:#5c5c5c}html body strong{color:#000}html body del{color:#5c5c5c}html body a:not([href]){color:inherit;text-decoration:none}html body a{color:#08c;text-decoration:none}html body a:hover{color:#00a3f5;text-decoration:none}html body img{max-width:100%}html body>p{margin-top:0;margin-bottom:16px;word-wrap:break-word}html body>ul,html body>ol{margin-bottom:16px}html body ul,html body ol{padding-left:2em}html body ul.no-list,html body ol.no-list{padding:0;list-style-type:none}html body ul ul,html body ul ol,html body ol ol,html body ol ul{margin-top:0;margin-bottom:0}html body li{margin-bottom:0}html body li.task-list-item{list-style:none}html body li>p{margin-top:0;margin-bottom:0}html body .task-list-item-checkbox{margin:0 .2em .25em -1.8em;vertical-align:middle}html body .task-list-item-checkbox:hover{cursor:pointer}html body blockquote{margin:16px 0;font-size:inherit;padding:0 15px;color:#5c5c5c;background-color:#f0f0f0;border-left:4px solid #d6d6d6}html body blockquote>:first-child{margin-top:0}html body blockquote>:last-child{margin-bottom:0}html body hr{height:4px;margin:32px 0;background-color:#d6d6d6;border:0 none}html body table{margin:10px 0 15px 0;border-collapse:collapse;border-spacing:0;display:block;width:100%;overflow:auto;word-break:normal;word-break:keep-all}html body table th{font-weight:bold;color:#000}html body table td,html body table th{border:1px solid #d6d6d6;padding:6px 13px}html body dl{padding:0}html body dl dt{padding:0;margin-top:16px;font-size:1em;font-style:italic;font-weight:bold}html body dl dd{padding:0 16px;margin-bottom:16px}html body code{font-family:Menlo,Monaco,Consolas,'Courier New',monospace;font-size:.85em !important;color:#000;background-color:#f0f0f0;border-radius:3px;padding:.2em 0}html body code::before,html body code::after{letter-spacing:-0.2em;content:"\00a0"}html body pre>code{padding:0;margin:0;font-size:.85em !important;word-break:normal;white-space:pre;background:transparent;border:0}html body .highlight{margin-bottom:16px}html body .highlight pre,html body pre{padding:1em;overflow:auto;font-size:.85em !important;line-height:1.45;border:#d6d6d6;border-radius:3px}html body .highlight pre{margin-bottom:0;word-break:normal}html body pre code,html body pre tt{display:inline;max-width:initial;padding:0;margin:0;overflow:initial;line-height:inherit;word-wrap:normal;background-color:transparent;border:0}html body pre code:before,html body pre tt:before,html body pre code:after,html body pre tt:after{content:normal}html body p,html body blockquote,html body ul,html body ol,html body dl,html body pre{margin-top:0;margin-bottom:16px}html body kbd{color:#000;border:1px solid #d6d6d6;border-bottom:2px solid #c7c7c7;padding:2px 4px;background-color:#f0f0f0;border-radius:3px}@media print{html body{background-color:#fff}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{color:#000;page-break-after:avoid}html body blockquote{color:#5c5c5c}html body pre{page-break-inside:avoid}html body table{display:table}html body img{display:block;max-width:100%;max-height:100%}html body pre,html body code{word-wrap:break-word;white-space:pre}}.markdown-preview{width:100%;height:100%;box-sizing:border-box}.markdown-preview .pagebreak,.markdown-preview .newpage{page-break-before:always}.markdown-preview pre.line-numbers{position:relative;padding-left:3.8em;counter-reset:linenumber}.markdown-preview pre.line-numbers>code{position:relative}.markdown-preview pre.line-numbers .line-numbers-rows{position:absolute;pointer-events:none;top:1em;font-size:100%;left:0;width:3em;letter-spacing:-1px;border-right:1px solid #999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.markdown-preview pre.line-numbers .line-numbers-rows>span{pointer-events:none;display:block;counter-increment:linenumber}.markdown-preview pre.line-numbers .line-numbers-rows>span:before{content:counter(linenumber);color:#999;display:block;padding-right:.8em;text-align:right}.markdown-preview .mathjax-exps .MathJax_Display{text-align:center !important}.markdown-preview:not([for="preview"]) .code-chunk .btn-group{display:none}.markdown-preview:not([for="preview"]) .code-chunk .status{display:none}.markdown-preview:not([for="preview"]) .code-chunk .output-div{margin-bottom:16px}.markdown-preview .md-toc{padding:0}.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link{display:inline;padding:.25rem 0}.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link p,.markdown-preview .md-toc .md-toc-link-wrapper .md-toc-link div{display:inline}.markdown-preview .md-toc .md-toc-link-wrapper.highlighted .md-toc-link{font-weight:800}.scrollbar-style::-webkit-scrollbar{width:8px}.scrollbar-style::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}.scrollbar-style::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,0.66);border:4px solid rgba(150,150,150,0.66);background-clip:content-box}html body[for="html-export"]:not([data-presentation-mode]){position:relative;width:100%;height:100%;top:0;left:0;margin:0;padding:0;overflow:auto}html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{position:relative;top:0}@media screen and (min-width:914px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{padding:2em calc(50% - 457px + 2em)}}@media screen and (max-width:914px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{font-size:14px !important;padding:1em}}@media print{html body[for="html-export"]:not([data-presentation-mode]) #sidebar-toc-btn{display:none}}html body[for="html-export"]:not([data-presentation-mode]) #sidebar-toc-btn{position:fixed;bottom:8px;left:8px;font-size:28px;cursor:pointer;color:inherit;z-index:99;width:32px;text-align:center;opacity:.4}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] #sidebar-toc-btn{opacity:1}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc{position:fixed;top:0;left:0;width:300px;height:100%;padding:32px 0 48px 0;font-size:14px;box-shadow:0 0 4px rgba(150,150,150,0.33);box-sizing:border-box;overflow:auto;background-color:inherit}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar{width:8px}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,0.66);border:4px solid rgba(150,150,150,0.66);background-clip:content-box}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc a{text-decoration:none}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc{padding:0 16px}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link{display:inline;padding:.25rem 0}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link p,html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper .md-toc-link div{display:inline}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc .md-toc .md-toc-link-wrapper.highlighted .md-toc-link{font-weight:800}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{left:300px;width:calc(100% -  300px);padding:2em calc(50% - 457px -  300px/2);margin:0;box-sizing:border-box}@media screen and (max-width:1274px){html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{width:100%}}html body[for="html-export"]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .markdown-preview{left:50%;transform:translateX(-50%)}html body[for="html-export"]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .md-sidebar-toc{display:none}
/* Please visit the URL below for more information: */
/*   https://shd101wyy.github.io/markdown-preview-enhanced/#/customize-css */

      </style>
    </head>
    <body for="html-export">
      <div class="mume markdown-preview  ">
      <center>
<h1>&#x6C88;&#x822A; YOLOX &#x8BAD;&#x7EC3;&#x548C;&#x8C03;&#x7528;&#x6307;&#x5357;</h1>
</center>
<h2 class="mume-header" id="1-%E4%B8%8B%E8%BD%BD%E6%B2%88%E8%88%AA%E7%9A%84%E5%BC%80%E6%BA%90-yolox-%E6%BA%90%E7%A0%81">1. &#x4E0B;&#x8F7D;&#x6C88;&#x822A;&#x7684;&#x5F00;&#x6E90; YOLOX &#x6E90;&#x7801;</h2>

<p>&#x4E0B;&#x8F7D;&#x5730;&#x5740;&#xFF1A; <a href="https://github.com/RangerOnMars/TUP-NN-Train">https://github.com/RangerOnMars/TUP-NN-Train</a></p>
<h2 class="mume-header" id="2-%E5%87%86%E5%A4%87%E6%95%B0%E6%8D%AE%E9%9B%86%E5%B0%86%E5%85%B6%E8%BD%AC%E5%8C%96%E4%B8%BA%E9%9C%80%E8%A6%81%E7%9A%84%E5%BD%A2%E5%BC%8F">2. &#x51C6;&#x5907;&#x6570;&#x636E;&#x96C6;&#xFF0C;&#x5C06;&#x5176;&#x8F6C;&#x5316;&#x4E3A;&#x9700;&#x8981;&#x7684;&#x5F62;&#x5F0F;</h2>

<p>&#x8FD9;&#x91CC;&#x51C6;&#x5907;&#x7684;&#x6570;&#x636E;&#x96C6;&#x662F; yolo-face &#x7684; txt &#x683C;&#x5F0F;&#xFF0C;&#x4E3A; 5 &#x4E2A;&#x5173;&#x952E;&#x70B9;&#x7684;&#x60C5;&#x51B5;&#x3002;&#x4F7F;&#x7528;&#x7684;&#x811A;&#x672C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre data-role="codeBlock" data-info="python" class="language-python"><span class="token triple-quoted-string string">&quot;&quot;&quot;
YOLO &#x683C;&#x5F0F;&#x7684;&#x6570;&#x636E;&#x96C6;&#x8F6C;&#x5316;&#x4E3A; COCO &#x683C;&#x5F0F;&#x7684;&#x6570;&#x636E;&#x96C6;
--root_dir &#x8F93;&#x5165;&#x6839;&#x8DEF;&#x5F84;
--save_path &#x4FDD;&#x5B58;&#x6587;&#x4EF6;&#x7684;&#x540D;&#x5B57;(&#x6CA1;&#x6709;random_split&#x65F6;&#x4F7F;&#x7528;)
--random_split &#x6709;&#x5219;&#x4F1A;&#x968F;&#x673A;&#x5212;&#x5206;&#x6570;&#x636E;&#x96C6;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x5206;&#x522B;&#x4FDD;&#x5B58;&#x4E3A;3&#x4E2A;&#x6587;&#x4EF6;&#x3002;
&quot;&quot;&quot;</span>

<span class="token keyword keyword-import">import</span> os
<span class="token keyword keyword-import">import</span> cv2
<span class="token keyword keyword-import">import</span> numpy <span class="token keyword keyword-as">as</span> np
<span class="token keyword keyword-import">import</span> json
<span class="token keyword keyword-from">from</span> tqdm <span class="token keyword keyword-import">import</span> tqdm
<span class="token keyword keyword-from">from</span> sklearn<span class="token punctuation">.</span>model_selection <span class="token keyword keyword-import">import</span> train_test_split
<span class="token keyword keyword-import">import</span> argparse

parser <span class="token operator">=</span> argparse<span class="token punctuation">.</span>ArgumentParser<span class="token punctuation">(</span><span class="token punctuation">)</span>
parser<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">&apos;--root_dir&apos;</span><span class="token punctuation">,</span> default<span class="token operator">=</span><span class="token string">&apos;./data&apos;</span><span class="token punctuation">,</span><span class="token builtin">type</span><span class="token operator">=</span><span class="token builtin">str</span><span class="token punctuation">,</span> <span class="token builtin">help</span><span class="token operator">=</span><span class="token string">&quot;root path of images and labels, include ./images and ./labels and classes.txt&quot;</span><span class="token punctuation">)</span>
parser<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">&apos;--save_path&apos;</span><span class="token punctuation">,</span> <span class="token builtin">type</span><span class="token operator">=</span><span class="token builtin">str</span><span class="token punctuation">,</span>default<span class="token operator">=</span><span class="token string">&apos;./train.json&apos;</span><span class="token punctuation">,</span> <span class="token builtin">help</span><span class="token operator">=</span><span class="token string">&quot;if not split the dataset, give a path to a json file&quot;</span><span class="token punctuation">)</span>
parser<span class="token punctuation">.</span>add_argument<span class="token punctuation">(</span><span class="token string">&apos;--random_split&apos;</span><span class="token punctuation">,</span> action<span class="token operator">=</span><span class="token string">&apos;store_true&apos;</span><span class="token punctuation">,</span> <span class="token builtin">help</span><span class="token operator">=</span><span class="token string">&quot;random split the dataset, default ratio is 8:1:1&quot;</span><span class="token punctuation">)</span>
arg <span class="token operator">=</span> parser<span class="token punctuation">.</span>parse_args<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token keyword keyword-def">def</span> <span class="token function">train_test_val_split</span><span class="token punctuation">(</span>img_paths<span class="token punctuation">,</span>ratio_train<span class="token operator">=</span><span class="token number">0.1</span><span class="token punctuation">,</span>ratio_test<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span>ratio_val<span class="token operator">=</span><span class="token number">0.2</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># &#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x6570;&#x636E;&#x96C6;&#x5212;&#x5206;&#x7684;&#x6BD4;&#x4F8B;&#x3002;</span>
    <span class="token keyword keyword-assert">assert</span> <span class="token builtin">int</span><span class="token punctuation">(</span>ratio_train<span class="token operator">+</span>ratio_test<span class="token operator">+</span>ratio_val<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">1</span>
    train_img<span class="token punctuation">,</span> middle_img <span class="token operator">=</span> train_test_split<span class="token punctuation">(</span>img_paths<span class="token punctuation">,</span>test_size<span class="token operator">=</span><span class="token number">1</span><span class="token operator">-</span>ratio_train<span class="token punctuation">,</span> random_state<span class="token operator">=</span><span class="token number">233</span><span class="token punctuation">)</span>
    ratio<span class="token operator">=</span>ratio_val<span class="token operator">/</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">-</span>ratio_train<span class="token punctuation">)</span>
    val_img<span class="token punctuation">,</span> test_img  <span class="token operator">=</span>train_test_split<span class="token punctuation">(</span>middle_img<span class="token punctuation">,</span>test_size<span class="token operator">=</span>ratio<span class="token punctuation">,</span> random_state<span class="token operator">=</span><span class="token number">233</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">&quot;NUMS of train:val:test = {}:{}:{}&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>train_img<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">len</span><span class="token punctuation">(</span>val_img<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">len</span><span class="token punctuation">(</span>test_img<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-return">return</span> train_img<span class="token punctuation">,</span> val_img<span class="token punctuation">,</span> test_img
    
<span class="token keyword keyword-def">def</span> <span class="token function">train_val_split</span><span class="token punctuation">(</span>img_paths<span class="token punctuation">,</span>ratio_train<span class="token operator">=</span><span class="token number">0.1</span><span class="token punctuation">,</span>ratio_val<span class="token operator">=</span><span class="token number">0.2</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># &#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x6570;&#x636E;&#x96C6;&#x5212;&#x5206;&#x7684;&#x6BD4;&#x4F8B;&#x3002;</span>
    train_img<span class="token punctuation">,</span> val_img <span class="token operator">=</span> train_test_split<span class="token punctuation">(</span>img_paths<span class="token punctuation">,</span>test_size<span class="token operator">=</span><span class="token number">1</span><span class="token operator">-</span>ratio_train<span class="token punctuation">,</span> random_state<span class="token operator">=</span><span class="token number">233</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">&quot;NUMS of train:val = {}:{}&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>train_img<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token builtin">len</span><span class="token punctuation">(</span>val_img<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-return">return</span> train_img<span class="token punctuation">,</span> val_img


<span class="token keyword keyword-def">def</span> <span class="token function">yolo2coco</span><span class="token punctuation">(</span>root_path<span class="token punctuation">,</span> random_split<span class="token punctuation">)</span><span class="token punctuation">:</span>
    originLabelsDir <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root_path<span class="token punctuation">,</span> <span class="token string">&apos;labels&apos;</span><span class="token punctuation">)</span>                                        
    originImagesDir <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root_path<span class="token punctuation">,</span> <span class="token string">&apos;images&apos;</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-with">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root_path<span class="token punctuation">,</span> <span class="token string">&apos;classes.txt&apos;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword keyword-as">as</span> f<span class="token punctuation">:</span>
        classes <span class="token operator">=</span> f<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment"># images dir name</span>
    indexes <span class="token operator">=</span> os<span class="token punctuation">.</span>listdir<span class="token punctuation">(</span>originImagesDir<span class="token punctuation">)</span>

    <span class="token keyword keyword-if">if</span> random_split<span class="token punctuation">:</span>
        <span class="token comment"># &#x7528;&#x4E8E;&#x4FDD;&#x5B58;&#x6240;&#x6709;&#x6570;&#x636E;&#x7684;&#x56FE;&#x7247;&#x4FE1;&#x606F;&#x548C;&#x6807;&#x6CE8;&#x4FE1;&#x606F;</span>
        train_dataset <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">&apos;categories&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">&apos;annotations&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">&apos;images&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
        val_dataset <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">&apos;categories&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">&apos;annotations&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">&apos;images&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
        test_dataset <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">&apos;categories&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">&apos;annotations&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">&apos;images&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">}</span>

        <span class="token comment"># &#x5EFA;&#x7ACB;&#x7C7B;&#x522B;&#x6807;&#x7B7E;&#x548C;&#x6570;&#x5B57;id&#x7684;&#x5BF9;&#x5E94;&#x5173;&#x7CFB;, &#x7C7B;&#x522B;id&#x4ECE;0&#x5F00;&#x59CB;&#x3002;</span>
        <span class="token keyword keyword-for">for</span> i<span class="token punctuation">,</span> cls <span class="token keyword keyword-in">in</span> <span class="token builtin">enumerate</span><span class="token punctuation">(</span>classes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            train_dataset<span class="token punctuation">[</span><span class="token string">&apos;categories&apos;</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">&apos;id&apos;</span><span class="token punctuation">:</span> i<span class="token punctuation">,</span> <span class="token string">&apos;name&apos;</span><span class="token punctuation">:</span> cls<span class="token punctuation">,</span> <span class="token string">&apos;supercategory&apos;</span><span class="token punctuation">:</span> <span class="token string">&apos;mark&apos;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
            val_dataset<span class="token punctuation">[</span><span class="token string">&apos;categories&apos;</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">&apos;id&apos;</span><span class="token punctuation">:</span> i<span class="token punctuation">,</span> <span class="token string">&apos;name&apos;</span><span class="token punctuation">:</span> cls<span class="token punctuation">,</span> <span class="token string">&apos;supercategory&apos;</span><span class="token punctuation">:</span> <span class="token string">&apos;mark&apos;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
            test_dataset<span class="token punctuation">[</span><span class="token string">&apos;categories&apos;</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">&apos;id&apos;</span><span class="token punctuation">:</span> i<span class="token punctuation">,</span> <span class="token string">&apos;name&apos;</span><span class="token punctuation">:</span> cls<span class="token punctuation">,</span> <span class="token string">&apos;supercategory&apos;</span><span class="token punctuation">:</span> <span class="token string">&apos;mark&apos;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment"># &#x6807;&#x8BB0; 1</span>
        train_img<span class="token punctuation">,</span> val_img <span class="token operator">=</span> train_val_split<span class="token punctuation">(</span>indexes<span class="token punctuation">,</span><span class="token number">0.95</span><span class="token punctuation">,</span><span class="token number">0.05</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
        dataset <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">&apos;categories&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">&apos;annotations&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">&apos;images&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">}</span>
        <span class="token keyword keyword-for">for</span> i<span class="token punctuation">,</span> cls <span class="token keyword keyword-in">in</span> <span class="token builtin">enumerate</span><span class="token punctuation">(</span>classes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            dataset<span class="token punctuation">[</span><span class="token string">&apos;categories&apos;</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">&apos;id&apos;</span><span class="token punctuation">:</span> i<span class="token punctuation">,</span> <span class="token string">&apos;name&apos;</span><span class="token punctuation">:</span> cls<span class="token punctuation">,</span> <span class="token string">&apos;supercategory&apos;</span><span class="token punctuation">:</span> <span class="token string">&apos;mark&apos;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
    
    <span class="token comment"># &#x6807;&#x6CE8;&#x7684;id</span>
    ann_id_cnt <span class="token operator">=</span> <span class="token number">0</span>
    <span class="token keyword keyword-for">for</span> k<span class="token punctuation">,</span> index <span class="token keyword keyword-in">in</span> <span class="token builtin">enumerate</span><span class="token punctuation">(</span>tqdm<span class="token punctuation">(</span>indexes<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment"># &#x652F;&#x6301; png jpg &#x683C;&#x5F0F;&#x7684;&#x56FE;&#x7247;&#x3002;</span>
        txtFile <span class="token operator">=</span> index<span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">&apos;images&apos;</span><span class="token punctuation">,</span><span class="token string">&apos;txt&apos;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">&apos;.jpg&apos;</span><span class="token punctuation">,</span><span class="token string">&apos;.txt&apos;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">&apos;.png&apos;</span><span class="token punctuation">,</span><span class="token string">&apos;.txt&apos;</span><span class="token punctuation">)</span>
        <span class="token comment"># &#x8BFB;&#x53D6;&#x56FE;&#x50CF;&#x7684;&#x5BBD;&#x548C;&#x9AD8;</span>
        im <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root_path<span class="token punctuation">,</span> <span class="token string">&apos;images/&apos;</span><span class="token punctuation">)</span> <span class="token operator">+</span> index<span class="token punctuation">)</span>
        height<span class="token punctuation">,</span> width<span class="token punctuation">,</span> _ <span class="token operator">=</span> im<span class="token punctuation">.</span>shape
        <span class="token keyword keyword-if">if</span> random_split<span class="token punctuation">:</span>
            <span class="token comment"># &#x5207;&#x6362;dataset&#x7684;&#x5F15;&#x7528;&#x5BF9;&#x8C61;&#xFF0C;&#x4ECE;&#x800C;&#x5212;&#x5206;&#x6570;&#x636E;&#x96C6;</span>
                <span class="token keyword keyword-if">if</span> index <span class="token keyword keyword-in">in</span> train_img<span class="token punctuation">:</span>
                    dataset <span class="token operator">=</span> train_dataset
                <span class="token keyword keyword-elif">elif</span> index <span class="token keyword keyword-in">in</span> val_img<span class="token punctuation">:</span>
                    dataset <span class="token operator">=</span> val_dataset
        <span class="token comment"># &#x6DFB;&#x52A0;&#x56FE;&#x50CF;&#x7684;&#x4FE1;&#x606F;</span>
        dataset<span class="token punctuation">[</span><span class="token string">&apos;images&apos;</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">&apos;file_name&apos;</span><span class="token punctuation">:</span> index<span class="token punctuation">,</span>
                                    <span class="token string">&apos;id&apos;</span><span class="token punctuation">:</span> k<span class="token punctuation">,</span>
                                    <span class="token string">&apos;width&apos;</span><span class="token punctuation">:</span> width<span class="token punctuation">,</span>
                                    <span class="token string">&apos;height&apos;</span><span class="token punctuation">:</span> height<span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token keyword keyword-if">if</span> <span class="token keyword keyword-not">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>originLabelsDir<span class="token punctuation">,</span> txtFile<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token comment"># &#x5982;&#x6CA1;&#x6807;&#x7B7E;&#xFF0C;&#x8DF3;&#x8FC7;&#xFF0C;&#x53EA;&#x4FDD;&#x7559;&#x56FE;&#x7247;&#x4FE1;&#x606F;&#x3002;</span>
            <span class="token keyword keyword-continue">continue</span>
        <span class="token comment"># print(txtFile)</span>
        <span class="token keyword keyword-with">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>originLabelsDir<span class="token punctuation">,</span> txtFile<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&apos;r&apos;</span><span class="token punctuation">)</span> <span class="token keyword keyword-as">as</span> fr<span class="token punctuation">:</span>
            labelList <span class="token operator">=</span> fr<span class="token punctuation">.</span>readlines<span class="token punctuation">(</span><span class="token punctuation">)</span>
            
<span class="token comment"># &#x6807;&#x8BB0; 2</span>
            <span class="token keyword keyword-for">for</span> label <span class="token keyword keyword-in">in</span> labelList<span class="token punctuation">:</span>
                label <span class="token operator">=</span> label<span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">&quot; &quot;</span><span class="token punctuation">)</span>
                H<span class="token punctuation">,</span> W<span class="token punctuation">,</span> _ <span class="token operator">=</span> im<span class="token punctuation">.</span>shape
                x  <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> W
                y  <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> H
                w  <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> W
                h  <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> H
                x1 <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> W
                y1 <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> H
                x2 <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> W
                y2 <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> H
                x3 <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> W
                y3 <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> H
                x4 <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> W
                y4 <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> H
                x5 <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">13</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> W
                y5 <span class="token operator">=</span> <span class="token builtin">float</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">14</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">*</span> H


                keypoints <span class="token operator">=</span> np<span class="token punctuation">.</span>array<span class="token punctuation">(</span><span class="token punctuation">[</span>x1<span class="token punctuation">,</span> y1<span class="token punctuation">,</span> x2<span class="token punctuation">,</span> y2<span class="token punctuation">,</span> x3<span class="token punctuation">,</span> y3<span class="token punctuation">,</span> x4<span class="token punctuation">,</span> y4<span class="token punctuation">,</span> x5<span class="token punctuation">,</span> y5<span class="token punctuation">]</span><span class="token punctuation">)</span>
                num_keypoints <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>keypoints<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span>

                keypoints <span class="token operator">=</span> keypoints<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span>
                keypoints_type <span class="token operator">=</span> <span class="token number">2</span> <span class="token operator">*</span> np<span class="token punctuation">.</span>ones<span class="token punctuation">(</span><span class="token punctuation">(</span>num_keypoints<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                keypoints <span class="token operator">=</span> np<span class="token punctuation">.</span>concatenate<span class="token punctuation">(</span><span class="token punctuation">(</span>keypoints<span class="token punctuation">,</span>keypoints_type<span class="token punctuation">)</span><span class="token punctuation">,</span>axis<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
                keypoints <span class="token operator">=</span> keypoints<span class="token punctuation">.</span>reshape<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>tolist<span class="token punctuation">(</span><span class="token punctuation">)</span>

                <span class="token comment"># &#x6807;&#x7B7E;&#x5E8F;&#x53F7;&#x4ECE;0&#x5F00;&#x59CB;&#x8BA1;&#x7B97;, coco2017&#x6570;&#x636E;&#x96C6;&#x6807;&#x53F7;&#x6DF7;&#x4E71;&#xFF0C;&#x4E0D;&#x7BA1;&#x5B83;&#x4E86;&#x3002;</span>
                cls_id <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>label<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>   
                width <span class="token operator">=</span> w
                height <span class="token operator">=</span> h
                dataset<span class="token punctuation">[</span><span class="token string">&apos;annotations&apos;</span><span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">{</span>
                    <span class="token string">&apos;area&apos;</span><span class="token punctuation">:</span> width <span class="token operator">*</span> height<span class="token punctuation">,</span>
                    <span class="token string">&apos;bbox&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>x<span class="token punctuation">,</span> y <span class="token punctuation">,</span> width<span class="token punctuation">,</span> height<span class="token punctuation">]</span><span class="token punctuation">,</span>
                    <span class="token string">&apos;category_id&apos;</span><span class="token punctuation">:</span> cls_id<span class="token punctuation">,</span>
                    <span class="token string">&apos;id&apos;</span><span class="token punctuation">:</span> ann_id_cnt<span class="token punctuation">,</span>
                    <span class="token string">&apos;image_id&apos;</span><span class="token punctuation">:</span> k<span class="token punctuation">,</span>
                    <span class="token string">&apos;iscrowd&apos;</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
                    <span class="token comment"># mask, &#x77E9;&#x5F62;&#x662F;&#x4ECE;&#x5DE6;&#x4E0A;&#x89D2;&#x70B9;&#x6309;&#x987A;&#x65F6;&#x9488;&#x7684;&#x56DB;&#x4E2A;&#x9876;&#x70B9;</span>
                    <span class="token string">&apos;segmentation&apos;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>x1<span class="token punctuation">,</span> y1<span class="token punctuation">,</span> x2<span class="token punctuation">,</span> y2<span class="token punctuation">,</span> x3<span class="token punctuation">,</span> y3<span class="token punctuation">,</span> x4<span class="token punctuation">,</span> y4<span class="token punctuation">,</span> x5<span class="token punctuation">,</span> y5<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                    <span class="token string">&apos;num_keypoints&apos;</span><span class="token punctuation">:</span> num_keypoints<span class="token punctuation">,</span>
                    <span class="token string">&apos;keypoints&apos;</span><span class="token punctuation">:</span> keypoints
                <span class="token punctuation">}</span><span class="token punctuation">)</span>
                ann_id_cnt <span class="token operator">+=</span> <span class="token number">1</span>

    <span class="token comment"># &#x4FDD;&#x5B58;&#x7ED3;&#x679C;</span>
    folder <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root_path<span class="token punctuation">,</span> <span class="token string">&apos;annotations&apos;</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-if">if</span> <span class="token keyword keyword-not">not</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>folder<span class="token punctuation">)</span><span class="token punctuation">:</span>
        os<span class="token punctuation">.</span>makedirs<span class="token punctuation">(</span>folder<span class="token punctuation">)</span>
    <span class="token keyword keyword-if">if</span> random_split<span class="token punctuation">:</span>
        <span class="token keyword keyword-for">for</span> phase <span class="token keyword keyword-in">in</span> <span class="token punctuation">[</span><span class="token string">&apos;instances_train2017&apos;</span><span class="token punctuation">,</span><span class="token string">&apos;instances_val2017&apos;</span><span class="token punctuation">,</span><span class="token string">&apos;test&apos;</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
            json_name <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root_path<span class="token punctuation">,</span> <span class="token string">&apos;annotations/{}.json&apos;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>phase<span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token keyword keyword-with">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>json_name<span class="token punctuation">,</span> <span class="token string">&apos;w&apos;</span><span class="token punctuation">)</span> <span class="token keyword keyword-as">as</span> f<span class="token punctuation">:</span>
                <span class="token keyword keyword-if">if</span> phase <span class="token operator">==</span> <span class="token string">&apos;instances_train2017&apos;</span><span class="token punctuation">:</span>
                    json<span class="token punctuation">.</span>dump<span class="token punctuation">(</span>train_dataset<span class="token punctuation">,</span> f<span class="token punctuation">)</span>
                <span class="token keyword keyword-elif">elif</span> phase <span class="token operator">==</span> <span class="token string">&apos;instances_val2017&apos;</span><span class="token punctuation">:</span>
                    json<span class="token punctuation">.</span>dump<span class="token punctuation">(</span>val_dataset<span class="token punctuation">,</span> f<span class="token punctuation">)</span>
                <span class="token keyword keyword-elif">elif</span> phase <span class="token operator">==</span> <span class="token string">&apos;test&apos;</span><span class="token punctuation">:</span>
                    json<span class="token punctuation">.</span>dump<span class="token punctuation">(</span>test_dataset<span class="token punctuation">,</span> f<span class="token punctuation">)</span>
            <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">&apos;Save annotation to {}&apos;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>json_name<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-else">else</span><span class="token punctuation">:</span>
        json_name <span class="token operator">=</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>join<span class="token punctuation">(</span>root_path<span class="token punctuation">,</span> <span class="token string">&apos;annotations/{}&apos;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>arg<span class="token punctuation">.</span>save_path<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword keyword-with">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>json_name<span class="token punctuation">,</span> <span class="token string">&apos;w&apos;</span><span class="token punctuation">)</span> <span class="token keyword keyword-as">as</span> f<span class="token punctuation">:</span>
            json<span class="token punctuation">.</span>dump<span class="token punctuation">(</span>dataset<span class="token punctuation">,</span> f<span class="token punctuation">)</span>
            <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">&apos;Save annotation to {}&apos;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>json_name<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword keyword-if">if</span> __name__ <span class="token operator">==</span> <span class="token string">&quot;__main__&quot;</span><span class="token punctuation">:</span>
    root_path <span class="token operator">=</span> arg<span class="token punctuation">.</span>root_dir
    <span class="token keyword keyword-assert">assert</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>exists<span class="token punctuation">(</span>root_path<span class="token punctuation">)</span>
    random_split <span class="token operator">=</span> arg<span class="token punctuation">.</span>random_split
    <span class="token keyword keyword-print">print</span><span class="token punctuation">(</span><span class="token string">&quot;Loading data from &quot;</span><span class="token punctuation">,</span>root_path<span class="token punctuation">,</span><span class="token string">&quot;\nWhether to split the data:&quot;</span><span class="token punctuation">,</span>random_split<span class="token punctuation">)</span>
    yolo2coco<span class="token punctuation">(</span>root_path<span class="token punctuation">,</span>random_split<span class="token punctuation">)</span>
</pre><ul>
<li><strong>&#x6807;&#x8BB0;1</strong>&#x5904;&#x4FEE;&#x6539;&#x6240;&#x5212;&#x5206;&#x7684;&#x8BAD;&#x7EC3;&#x96C6;&#x548C;&#x9A8C;&#x8BC1;&#x96C6;&#x7684;&#x6BD4;&#x4F8B;</li>
<li><strong>&#x6807;&#x8BB0;2</strong>&#x5904;&#x4FEE;&#x6539;&#x68C0;&#x6D4B;&#x5173;&#x952E;&#x70B9;&#x7684;&#x76F8;&#x5173;&#x4FE1;&#x606F;&#x3002;&#x8BE5;&#x4EE3;&#x7801;&#x662F;&#x4EE5; yolo-face &#x683C;&#x5F0F;&#x7684;&#x6570;&#x636E;&#x96C6;&#x8FDB;&#x884C;&#x8F6C;&#x5316;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#x8F6C;&#x5316;&#x5176;&#x5B83;&#x683C;&#x5F0F;&#x7684;&#x6807;&#x7B7E;&#x53EF;&#x4EE5;&#x81EA;&#x884C;&#x4FEE;&#x6539;&#x76F8;&#x5173;&#x5185;&#x5BB9;&#x3002;</li>
</ul>
<p>&#x4E4B;&#x540E;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x5939; data&#xFF0C;&#x5C06;&#x56FE;&#x7247;&#x548C; txt &#x6807;&#x7B7E;&#x6309;&#x7167;&#x4EE5;&#x4E0B;&#x8DEF;&#x5F84;&#x5B58;&#x653E;&#xFF1A;</p>
<pre data-role="codeBlock" data-info class="language-"><code>data/
&#x251C;&#x2500;&#x2500; images
    (&#x6B64;&#x5904;&#x5B58;&#x653E;&#x6240;&#x6709;&#x7684;&#x56FE;&#x7247;)
&#x2514;&#x2500;&#x2500; labels
    (&#x6B64;&#x5904;&#x5B58;&#x653E;&#x6240;&#x6709;&#x7684;&#x6807;&#x7B7E;)
</code></pre><p>&#x6267;&#x884C;&#x811A;&#x672C;</p>
<pre data-role="codeBlock" data-info="python" class="language-python">python3 yolo2coco<span class="token punctuation">.</span>py <span class="token operator">-</span><span class="token operator">-</span>root_dir <span class="token punctuation">.</span> <span class="token operator">-</span><span class="token operator">-</span>random_split
<span class="token comment"># root_dir &#x4E3A; images/ &#x6587;&#x4EF6;&#x5939;&#x548C; labels/ &#x6587;&#x4EF6;&#x5939;&#x6240;&#x5728;&#x7684;&#x76EE;&#x5F55;</span>
</pre><p>&#x6267;&#x884C;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x5728; annotations &#x6587;&#x4EF6;&#x5939;&#x4E0B;&#x770B;&#x5230;&#x4E09;&#x4E2A; .json &#x6587;&#x4EF6;</p>
<ul>
<li>instances_train2017.json</li>
<li>instances_val2017.json</li>
<li>test.json&#xFF08;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x6CA1;&#x4EC0;&#x4E48;&#x7528;&#xFF09;</li>
</ul>
<h2 class="mume-header" id="3-%E5%BC%80%E5%A7%8B%E8%AE%AD%E7%BB%83">3. &#x5F00;&#x59CB;&#x8BAD;&#x7EC3;</h2>

<p>&#x8BAD;&#x7EC3;&#x90E8;&#x5206;&#x53C2;&#x8003;&#x8BE5;&#x4ED3;&#x5E93;&#x7684; Wiki &#x6559;&#x7A0B;&#x5373;&#x53EF;&#xFF1A;</p>
<p><a href="https://github.com/RangerOnMars/TUP-NN-Train/wiki/Tutorial">https://github.com/RangerOnMars/TUP-NN-Train/wiki/Tutorial</a></p>
<h2 class="mume-header" id="4-%E8%B0%83%E7%94%A8%E6%A8%A1%E5%9E%8B">4. &#x8C03;&#x7528;&#x6A21;&#x578B;</h2>

<p>&#x9996;&#x5148;&#x9700;&#x8981;&#x628A;&#x8BAD;&#x7EC3;&#x5F97;&#x5230;&#x7684; .pth &#x6A21;&#x578B;&#x8F6C;&#x5316;&#x4E3A; .onnx &#x6A21;&#x578B;&#xFF0C;&#x4E4B;&#x540E;&#x518D;&#x8F6C;&#x5316;&#x6210; IR &#x6A21;&#x578B;&#xFF08;&#x5305;&#x542B;&#x4E00;&#x4E2A; .xml &#x6587;&#x4EF6;&#x548C; .bin &#x6587;&#x4EF6;&#xFF09;&#x3002;</p>
<p>&#x7531;&#x4E8E;&#x59CB;&#x7EC8;&#x6CA1;&#x6709;&#x8BFB;&#x61C2; YOLOX &#x7684; ONNX &#x7F51;&#x7EDC;&#x7684;&#x8F93;&#x51FA;&#x4EE3;&#x8868;&#x4EC0;&#x4E48;&#xFF0C;&#x56E0;&#x6B64;&#x53EA;&#x80FD;&#x4F7F;&#x7528;&#x6C88;&#x822A;&#x7ED9;&#x7684; OpenVINO &#x7684;&#x8C03;&#x7528;&#x4EE3;&#x7801;&#x3002;</p>
<p>&#x5934;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre data-role="codeBlock" data-info="cpp" class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">RUNE_DETECTOR__RUNE_DETECTOR_HPP_</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">RUNE_DETECTOR__RUNE_DETECTOR_HPP_</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;Eigen/Core&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;cv_bridge/cv_bridge.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fmt/color.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fmt/format.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;inference_engine.hpp&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;ros/ros.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sensor_msgs/Image.h&gt;</span></span>

<span class="token keyword keyword-namespace">namespace</span> rune_detector <span class="token punctuation">{</span>

<span class="token comment">// constexpr int ERED = 0;</span>
<span class="token comment">// constexpr int BLUE = 1;</span>
<span class="token keyword keyword-struct">struct</span> <span class="token class-name">detectParam</span><span class="token punctuation">{</span>
	<span class="token keyword keyword-bool">bool</span> debug<span class="token punctuation">;</span>
	<span class="token keyword keyword-float">float</span> input_width<span class="token punctuation">;</span>
	<span class="token keyword keyword-float">float</span> input_height<span class="token punctuation">;</span>
	<span class="token keyword keyword-float">float</span> score_threshold<span class="token punctuation">;</span>
	<span class="token keyword keyword-float">float</span> nms_threshold<span class="token punctuation">;</span>
	<span class="token keyword keyword-float">float</span> conf_threshold<span class="token punctuation">;</span>
	<span class="token keyword keyword-int">int</span> device<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword keyword-struct">struct</span> <span class="token class-name">Detection</span> <span class="token punctuation">{</span>
  <span class="token keyword keyword-int">int</span> class_id<span class="token punctuation">;</span>
  <span class="token keyword keyword-float">float</span> confidence<span class="token punctuation">;</span>
  cv<span class="token double-colon punctuation">::</span>Rect box<span class="token punctuation">;</span>
  std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>cv<span class="token double-colon punctuation">::</span>Point2f<span class="token operator">&gt;</span> points<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword keyword-struct">struct</span> <span class="token class-name">GridAndStride</span> <span class="token punctuation">{</span>
  <span class="token keyword keyword-int">int</span> grid0<span class="token punctuation">;</span>
  <span class="token keyword keyword-int">int</span> grid1<span class="token punctuation">;</span>
  <span class="token keyword keyword-int">int</span> stride<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment">// yolox5&#x70B9;+OpenVINO&#x52A0;&#x901F;</span>
<span class="token keyword keyword-class">class</span> <span class="token class-name">VINO_Detector</span> <span class="token punctuation">{</span>
<span class="token keyword keyword-public">public</span><span class="token operator">:</span>
  <span class="token keyword keyword-using">using</span> Ptr <span class="token operator">=</span> std<span class="token double-colon punctuation">::</span>unique_ptr<span class="token operator">&lt;</span>VINO_Detector<span class="token operator">&gt;</span><span class="token punctuation">;</span>

  <span class="token function">VINO_Detector</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  <span class="token function">VINO_Detector</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> std<span class="token double-colon punctuation">::</span>string <span class="token operator">&amp;</span>model_name<span class="token punctuation">,</span> <span class="token keyword keyword-bool">bool</span> debug<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// bool init(const std::string &amp;model_name,const detectParam &amp;param);</span>

  <span class="token operator">~</span><span class="token function">VINO_Detector</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-bool">bool</span> <span class="token function">detect</span><span class="token punctuation">(</span>cv<span class="token double-colon punctuation">::</span>Mat <span class="token operator">&amp;</span>src<span class="token punctuation">,</span> std<span class="token double-colon punctuation">::</span>array<span class="token operator">&lt;</span>cv<span class="token double-colon punctuation">::</span>Point2f<span class="token punctuation">,</span> <span class="token number">5</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword keyword-private">private</span><span class="token operator">:</span>
  <span class="token keyword keyword-bool">bool</span> <span class="token function">initModel</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> std<span class="token double-colon punctuation">::</span>string <span class="token operator">&amp;</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-void">void</span> <span class="token function">debugProcess</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> cv<span class="token double-colon punctuation">::</span>Mat <span class="token operator">&amp;</span>src<span class="token punctuation">,</span> <span class="token keyword keyword-const">const</span> std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>Detection<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>objects<span class="token punctuation">,</span>
                    <span class="token keyword keyword-double">double</span> t<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-bool">bool</span> debug_<span class="token punctuation">;</span>

  InferenceEngine<span class="token double-colon punctuation">::</span>Core ie_<span class="token punctuation">;</span>
  InferenceEngine<span class="token double-colon punctuation">::</span>CNNNetwork network_<span class="token punctuation">;</span>                   <span class="token comment">// &#x7F51;&#x7EDC;</span>
  InferenceEngine<span class="token double-colon punctuation">::</span>ExecutableNetwork executable_network_<span class="token punctuation">;</span> <span class="token comment">// &#x53EF;&#x6267;&#x884C;&#x7F51;&#x7EDC;</span>
  InferenceEngine<span class="token double-colon punctuation">::</span>InferRequest infer_request_<span class="token punctuation">;</span>           <span class="token comment">// &#x63A8;&#x7406;&#x8BF7;&#x6C42;</span>
  InferenceEngine<span class="token double-colon punctuation">::</span>MemoryBlob<span class="token double-colon punctuation">::</span>CPtr moutput_<span class="token punctuation">;</span>
  std<span class="token double-colon punctuation">::</span>string input_name_<span class="token punctuation">;</span>
  std<span class="token double-colon punctuation">::</span>string output_name_<span class="token punctuation">;</span>

  ros<span class="token double-colon punctuation">::</span>NodeHandle n_<span class="token punctuation">;</span>
  ros<span class="token double-colon punctuation">::</span>Publisher debug_img_pub_<span class="token punctuation">;</span>
  <span class="token comment">// detectParam param_;</span>
  std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>std<span class="token double-colon punctuation">::</span>string<span class="token operator">&gt;</span> class_list_<span class="token punctuation">;</span>

  Eigen<span class="token double-colon punctuation">::</span>Matrix<span class="token operator">&lt;</span><span class="token keyword keyword-float">float</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token operator">&gt;</span> transfrom_matrix<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword keyword-const">const</span> std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>cv<span class="token double-colon punctuation">::</span>Scalar<span class="token operator">&gt;</span> colors <span class="token operator">=</span> <span class="token punctuation">{</span>
    cv<span class="token double-colon punctuation">::</span><span class="token function">Scalar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> cv<span class="token double-colon punctuation">::</span><span class="token function">Scalar</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> cv<span class="token double-colon punctuation">::</span><span class="token function">Scalar</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    cv<span class="token double-colon punctuation">::</span><span class="token function">Scalar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span> <span class="token comment">// namespace rune_detector</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span> <span class="token comment">// RUNE__RUNE_DETECTOR_HPP_</span></span>

</pre><p>&#x6E90;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre data-role="codeBlock" data-info="cpp" class="language-cpp"><span class="token comment">// Copyright (C) 2018-2021 Intel Corporation</span>
<span class="token comment">// SPDX-License-Identifier: Apache-2.0</span>
<span class="token comment">//</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&quot;rune_detector/rune_detector.hpp&quot;</span></span>

<span class="token keyword keyword-using">using</span> <span class="token keyword keyword-namespace">namespace</span> InferenceEngine<span class="token punctuation">;</span>
<span class="token keyword keyword-namespace">namespace</span> rune_detector <span class="token punctuation">{</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-constexpr">constexpr</span> <span class="token keyword keyword-int">int</span> INPUT_W <span class="token operator">=</span> <span class="token number">416</span><span class="token punctuation">;</span> <span class="token comment">// Width of input</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-constexpr">constexpr</span> <span class="token keyword keyword-int">int</span> INPUT_H <span class="token operator">=</span> <span class="token number">416</span><span class="token punctuation">;</span> <span class="token comment">// Height of input</span>
<span class="token comment">// static constexpr int INPUT_W = 416;    // Width of input</span>
<span class="token comment">// static constexpr int INPUT_H = 416;    // Height of input</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-constexpr">constexpr</span> <span class="token keyword keyword-int">int</span> NUM_CLASSES <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span> <span class="token comment">// Number of classes</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-constexpr">constexpr</span> <span class="token keyword keyword-int">int</span> NUM_COLORS <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>  <span class="token comment">// Number of color</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-constexpr">constexpr</span> <span class="token keyword keyword-int">int</span> TOPK <span class="token operator">=</span> <span class="token number">128</span><span class="token punctuation">;</span>      <span class="token comment">// TopK</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-constexpr">constexpr</span> <span class="token keyword keyword-float">float</span> NMS_THRESH <span class="token operator">=</span> <span class="token number">0.3</span><span class="token punctuation">;</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-constexpr">constexpr</span> <span class="token keyword keyword-float">float</span> BBOX_CONF_THRESH <span class="token operator">=</span> <span class="token number">0.2</span><span class="token punctuation">;</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-constexpr">constexpr</span> <span class="token keyword keyword-float">float</span> MERGE_CONF_ERROR <span class="token operator">=</span> <span class="token number">0.15</span><span class="token punctuation">;</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-constexpr">constexpr</span> <span class="token keyword keyword-float">float</span> MERGE_MIN_IOU <span class="token operator">=</span> <span class="token number">0.9</span><span class="token punctuation">;</span>

<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-inline">inline</span> <span class="token keyword keyword-int">int</span> <span class="token function">argmax</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> <span class="token keyword keyword-float">float</span> <span class="token operator">*</span>ptr<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> len<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword keyword-int">int</span> max_arg <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>ptr<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">&gt;</span> ptr<span class="token punctuation">[</span>max_arg<span class="token punctuation">]</span><span class="token punctuation">)</span>
      max_arg <span class="token operator">=</span> i<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword keyword-return">return</span> max_arg<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * @brief Resize the image using letterbox
 * @param img Image before resize
 * @param transform_matrix Transform Matrix of Resize
 * @return Image after resize
 */</span>
<span class="token keyword keyword-inline">inline</span> cv<span class="token double-colon punctuation">::</span>Mat <span class="token function">scaledResize</span><span class="token punctuation">(</span>cv<span class="token double-colon punctuation">::</span>Mat <span class="token operator">&amp;</span>img<span class="token punctuation">,</span>
                            Eigen<span class="token double-colon punctuation">::</span>Matrix<span class="token operator">&lt;</span><span class="token keyword keyword-float">float</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span>transform_matrix<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword keyword-float">float</span> r <span class="token operator">=</span> std<span class="token double-colon punctuation">::</span><span class="token function">min</span><span class="token punctuation">(</span>INPUT_W <span class="token operator">/</span> <span class="token punctuation">(</span>img<span class="token punctuation">.</span>cols <span class="token operator">*</span> <span class="token number">1.0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> INPUT_H <span class="token operator">/</span> <span class="token punctuation">(</span>img<span class="token punctuation">.</span>rows <span class="token operator">*</span> <span class="token number">1.0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-int">int</span> unpad_w <span class="token operator">=</span> r <span class="token operator">*</span> img<span class="token punctuation">.</span>cols<span class="token punctuation">;</span>
  <span class="token keyword keyword-int">int</span> unpad_h <span class="token operator">=</span> r <span class="token operator">*</span> img<span class="token punctuation">.</span>rows<span class="token punctuation">;</span>

  <span class="token keyword keyword-int">int</span> dw <span class="token operator">=</span> INPUT_W <span class="token operator">-</span> unpad_w<span class="token punctuation">;</span>
  <span class="token keyword keyword-int">int</span> dh <span class="token operator">=</span> INPUT_H <span class="token operator">-</span> unpad_h<span class="token punctuation">;</span>

  dw <span class="token operator">/=</span> <span class="token number">2</span><span class="token punctuation">;</span>
  dh <span class="token operator">/=</span> <span class="token number">2</span><span class="token punctuation">;</span>

  transform_matrix <span class="token operator">&lt;&lt;</span> <span class="token number">1.0</span> <span class="token operator">/</span> r<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span>dw <span class="token operator">/</span> r<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1.0</span> <span class="token operator">/</span> r<span class="token punctuation">,</span> <span class="token operator">-</span>dh <span class="token operator">/</span> r<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">;</span>

  cv<span class="token double-colon punctuation">::</span>Mat re<span class="token punctuation">;</span>
  cv<span class="token double-colon punctuation">::</span><span class="token function">resize</span><span class="token punctuation">(</span>img<span class="token punctuation">,</span> re<span class="token punctuation">,</span> cv<span class="token double-colon punctuation">::</span><span class="token function">Size</span><span class="token punctuation">(</span>unpad_w<span class="token punctuation">,</span> unpad_h<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  cv<span class="token double-colon punctuation">::</span>Mat out<span class="token punctuation">;</span>
  cv<span class="token double-colon punctuation">::</span><span class="token function">copyMakeBorder</span><span class="token punctuation">(</span>re<span class="token punctuation">,</span> out<span class="token punctuation">,</span> dh<span class="token punctuation">,</span> dh<span class="token punctuation">,</span> dw<span class="token punctuation">,</span> dw<span class="token punctuation">,</span> cv<span class="token double-colon punctuation">::</span>BORDER_CONSTANT<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword keyword-return">return</span> out<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * @brief Generate grids and stride.
 * @param target_w Width of input.
 * @param target_h Height of input.
 * @param strides A vector of stride.
 * @param grid_strides Grid stride generated in this function.
 */</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-void">void</span>
<span class="token function">generate_grids_and_stride</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> <span class="token keyword keyword-int">int</span> target_w<span class="token punctuation">,</span> <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-int">int</span> target_h<span class="token punctuation">,</span>
                          std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span><span class="token keyword keyword-int">int</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span>strides<span class="token punctuation">,</span>
                          std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>GridAndStride<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>grid_strides<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-auto">auto</span> stride <span class="token operator">:</span> strides<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword keyword-int">int</span> num_grid_w <span class="token operator">=</span> target_w <span class="token operator">/</span> stride<span class="token punctuation">;</span>
    <span class="token keyword keyword-int">int</span> num_grid_h <span class="token operator">=</span> target_h <span class="token operator">/</span> stride<span class="token punctuation">;</span>

    <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> g1 <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> g1 <span class="token operator">&lt;</span> num_grid_h<span class="token punctuation">;</span> g1<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> g0 <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> g0 <span class="token operator">&lt;</span> num_grid_w<span class="token punctuation">;</span> g0<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        grid_strides<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token punctuation">(</span>GridAndStride<span class="token punctuation">)</span><span class="token punctuation">{</span>g0<span class="token punctuation">,</span> g1<span class="token punctuation">,</span> stride<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * @brief Generate Proposal
 * @param grid_strides Grid strides
 * @param feat_ptr Original predition result.
 * @param prob_threshold Confidence Threshold.
 * @param objects Objects proposed.
 */</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-void">void</span> <span class="token function">generateYoloxProposals</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>GridAndStride<span class="token operator">&gt;</span> grid_strides<span class="token punctuation">,</span>
                                   <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-float">float</span> <span class="token operator">*</span>feat_ptr<span class="token punctuation">,</span>
                                   Eigen<span class="token double-colon punctuation">::</span>Matrix<span class="token operator">&lt;</span><span class="token keyword keyword-float">float</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span>transform_matrix<span class="token punctuation">,</span>
                                   <span class="token keyword keyword-float">float</span> prob_threshold<span class="token punctuation">,</span>
                                   std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>Detection<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>objects<span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-int">int</span> num_anchors <span class="token operator">=</span> grid_strides<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// Travel all the anchors</span>

  <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> anchor_idx <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> anchor_idx <span class="token operator">&lt;</span> num_anchors<span class="token punctuation">;</span> anchor_idx<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-int">int</span> grid0 <span class="token operator">=</span> grid_strides<span class="token punctuation">[</span>anchor_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>grid0<span class="token punctuation">;</span>
    <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-int">int</span> grid1 <span class="token operator">=</span> grid_strides<span class="token punctuation">[</span>anchor_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>grid1<span class="token punctuation">;</span>
    <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-int">int</span> stride <span class="token operator">=</span> grid_strides<span class="token punctuation">[</span>anchor_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>stride<span class="token punctuation">;</span>

<span class="token comment">//&#x6807;&#x8BB0; 1</span>
    <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-int">int</span> basic_pos <span class="token operator">=</span> anchor_idx <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token number">11</span> <span class="token operator">+</span> NUM_COLORS <span class="token operator">+</span> NUM_CLASSES<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// yolox/models/yolo_head.py decode logic</span>
    <span class="token comment">//  outputs[..., :2] = (outputs[..., :2] + grids) * strides</span>
    <span class="token comment">//  outputs[..., 2:4] = torch.exp(outputs[..., 2:4]) * strides</span>
    <span class="token keyword keyword-float">float</span> x_1 <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> grid0<span class="token punctuation">)</span> <span class="token operator">*</span> stride<span class="token punctuation">;</span>
    <span class="token keyword keyword-float">float</span> y_1 <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> grid1<span class="token punctuation">)</span> <span class="token operator">*</span> stride<span class="token punctuation">;</span>
    <span class="token keyword keyword-float">float</span> x_2 <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span> grid0<span class="token punctuation">)</span> <span class="token operator">*</span> stride<span class="token punctuation">;</span>
    <span class="token keyword keyword-float">float</span> y_2 <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">+</span> grid1<span class="token punctuation">)</span> <span class="token operator">*</span> stride<span class="token punctuation">;</span>
    <span class="token keyword keyword-float">float</span> x_3 <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">4</span><span class="token punctuation">]</span> <span class="token operator">+</span> grid0<span class="token punctuation">)</span> <span class="token operator">*</span> stride<span class="token punctuation">;</span>
    <span class="token keyword keyword-float">float</span> y_3 <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">5</span><span class="token punctuation">]</span> <span class="token operator">+</span> grid1<span class="token punctuation">)</span> <span class="token operator">*</span> stride<span class="token punctuation">;</span>
    <span class="token keyword keyword-float">float</span> x_4 <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">6</span><span class="token punctuation">]</span> <span class="token operator">+</span> grid0<span class="token punctuation">)</span> <span class="token operator">*</span> stride<span class="token punctuation">;</span>
    <span class="token keyword keyword-float">float</span> y_4 <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">7</span><span class="token punctuation">]</span> <span class="token operator">+</span> grid1<span class="token punctuation">)</span> <span class="token operator">*</span> stride<span class="token punctuation">;</span>
    <span class="token keyword keyword-float">float</span> x_5 <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">8</span><span class="token punctuation">]</span> <span class="token operator">+</span> grid0<span class="token punctuation">)</span> <span class="token operator">*</span> stride<span class="token punctuation">;</span>
    <span class="token keyword keyword-float">float</span> y_5 <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">9</span><span class="token punctuation">]</span> <span class="token operator">+</span> grid1<span class="token punctuation">)</span> <span class="token operator">*</span> stride<span class="token punctuation">;</span>

    <span class="token keyword keyword-int">int</span> box_color <span class="token operator">=</span> <span class="token function">argmax</span><span class="token punctuation">(</span>feat_ptr <span class="token operator">+</span> basic_pos <span class="token operator">+</span> <span class="token number">11</span><span class="token punctuation">,</span> NUM_COLORS<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-int">int</span> box_class <span class="token operator">=</span> <span class="token function">argmax</span><span class="token punctuation">(</span>feat_ptr <span class="token operator">+</span> basic_pos <span class="token operator">+</span> <span class="token number">11</span> <span class="token operator">+</span> NUM_COLORS<span class="token punctuation">,</span> NUM_CLASSES<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-float">float</span> box_objectness <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-float">float</span> color_conf <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">11</span> <span class="token operator">+</span> box_color<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-float">float</span> cls_conf <span class="token operator">=</span> <span class="token punctuation">(</span>feat_ptr<span class="token punctuation">[</span>basic_pos <span class="token operator">+</span> <span class="token number">11</span> <span class="token operator">+</span> NUM_COLORS <span class="token operator">+</span> box_class<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// float box_prob = (box_objectness + cls_conf + color_conf) / 3.0;</span>
    <span class="token keyword keyword-float">float</span> box_prob <span class="token operator">=</span> box_objectness<span class="token punctuation">;</span>

    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>box_prob <span class="token operator">&gt;=</span> prob_threshold<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      Detection obj<span class="token punctuation">;</span>

      Eigen<span class="token double-colon punctuation">::</span>Matrix<span class="token operator">&lt;</span><span class="token keyword keyword-float">float</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token operator">&gt;</span> apex_norm<span class="token punctuation">;</span>
      Eigen<span class="token double-colon punctuation">::</span>Matrix<span class="token operator">&lt;</span><span class="token keyword keyword-float">float</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token operator">&gt;</span> apex_dst<span class="token punctuation">;</span>

<span class="token comment">//&#x6807;&#x8BB0; 2</span>
      apex_norm <span class="token operator">&lt;&lt;</span> x_1<span class="token punctuation">,</span> x_2<span class="token punctuation">,</span> x_3<span class="token punctuation">,</span> x_4<span class="token punctuation">,</span> x_5<span class="token punctuation">,</span> y_1<span class="token punctuation">,</span> y_2<span class="token punctuation">,</span> y_3<span class="token punctuation">,</span> y_4<span class="token punctuation">,</span> y_5<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span>
          <span class="token number">1</span><span class="token punctuation">;</span>

      apex_dst <span class="token operator">=</span> transform_matrix <span class="token operator">*</span> apex_norm<span class="token punctuation">;</span>

      <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        obj<span class="token punctuation">.</span>points<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>cv<span class="token double-colon punctuation">::</span><span class="token function">Point2f</span><span class="token punctuation">(</span><span class="token function">apex_dst</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">apex_dst</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>

      obj<span class="token punctuation">.</span>box <span class="token operator">=</span> cv<span class="token double-colon punctuation">::</span><span class="token function">boundingRect</span><span class="token punctuation">(</span>obj<span class="token punctuation">.</span>points<span class="token punctuation">)</span><span class="token punctuation">;</span>

      obj<span class="token punctuation">.</span>class_id <span class="token operator">=</span> box_color <span class="token operator">*</span> <span class="token number">3</span> <span class="token operator">+</span> box_class<span class="token punctuation">;</span>
      obj<span class="token punctuation">.</span>confidence <span class="token operator">=</span> box_prob<span class="token punctuation">;</span>

      objects<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

  <span class="token punctuation">}</span> <span class="token comment">// point anchor loop</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * @brief Calculate intersection area between two objects.
 * @param a Object a.
 * @param b Object b.
 * @return Area of intersection.
 */</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-inline">inline</span> <span class="token keyword keyword-float">float</span> <span class="token function">intersection_area</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> Detection <span class="token operator">&amp;</span>a<span class="token punctuation">,</span> <span class="token keyword keyword-const">const</span> Detection <span class="token operator">&amp;</span>b<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  cv<span class="token double-colon punctuation">::</span>Rect_<span class="token operator">&lt;</span><span class="token keyword keyword-float">float</span><span class="token operator">&gt;</span> inter <span class="token operator">=</span> a<span class="token punctuation">.</span>box <span class="token operator">&amp;</span> b<span class="token punctuation">.</span>box<span class="token punctuation">;</span>
  <span class="token keyword keyword-return">return</span> inter<span class="token punctuation">.</span><span class="token function">area</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-void">void</span> <span class="token function">qsort_descent_inplace</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>Detection<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>faceobjects<span class="token punctuation">,</span> <span class="token keyword keyword-int">int</span> left<span class="token punctuation">,</span>
                                  <span class="token keyword keyword-int">int</span> right<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> left<span class="token punctuation">;</span>
  <span class="token keyword keyword-int">int</span> j <span class="token operator">=</span> right<span class="token punctuation">;</span>
  <span class="token keyword keyword-float">float</span> p <span class="token operator">=</span> faceobjects<span class="token punctuation">[</span><span class="token punctuation">(</span>left <span class="token operator">+</span> right<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span>confidence<span class="token punctuation">;</span>

  <span class="token keyword keyword-while">while</span> <span class="token punctuation">(</span>i <span class="token operator">&lt;=</span> j<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword keyword-while">while</span> <span class="token punctuation">(</span>faceobjects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>confidence <span class="token operator">&gt;</span> p<span class="token punctuation">)</span>
      i<span class="token operator">++</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-while">while</span> <span class="token punctuation">(</span>faceobjects<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">.</span>confidence <span class="token operator">&lt;</span> p<span class="token punctuation">)</span>
      j<span class="token operator">--</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>i <span class="token operator">&lt;=</span> j<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// swap</span>
      std<span class="token double-colon punctuation">::</span><span class="token function">swap</span><span class="token punctuation">(</span>faceobjects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> faceobjects<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

      i<span class="token operator">++</span><span class="token punctuation">;</span>
      j<span class="token operator">--</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">pragma</span> <span class="token expression">omp parallel sections</span></span>
  <span class="token punctuation">{</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">pragma</span> <span class="token expression">omp section</span></span>
    <span class="token punctuation">{</span>
      <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>left <span class="token operator">&lt;</span> j<span class="token punctuation">)</span>
        <span class="token function">qsort_descent_inplace</span><span class="token punctuation">(</span>faceobjects<span class="token punctuation">,</span> left<span class="token punctuation">,</span> j<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">pragma</span> <span class="token expression">omp section</span></span>
    <span class="token punctuation">{</span>
      <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>i <span class="token operator">&lt;</span> right<span class="token punctuation">)</span>
        <span class="token function">qsort_descent_inplace</span><span class="token punctuation">(</span>faceobjects<span class="token punctuation">,</span> i<span class="token punctuation">,</span> right<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-void">void</span> <span class="token function">qsort_descent_inplace</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>Detection<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>objects<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>objects<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-return">return</span><span class="token punctuation">;</span>

  <span class="token function">qsort_descent_inplace</span><span class="token punctuation">(</span>objects<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> objects<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-void">void</span> <span class="token function">nms_sorted_bboxes</span><span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>Detection<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>faceobjects<span class="token punctuation">,</span>
                              std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span><span class="token keyword keyword-int">int</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span>picked<span class="token punctuation">,</span> <span class="token keyword keyword-float">float</span> nms_threshold<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  picked<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-int">int</span> n <span class="token operator">=</span> faceobjects<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span><span class="token keyword keyword-float">float</span><span class="token operator">&gt;</span> <span class="token function">areas</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    areas<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> faceobjects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>box<span class="token punctuation">.</span><span class="token function">area</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    Detection <span class="token operator">&amp;</span>a <span class="token operator">=</span> faceobjects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>

    <span class="token keyword keyword-int">int</span> keep <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span><span class="token punctuation">)</span>picked<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      Detection <span class="token operator">&amp;</span>b <span class="token operator">=</span> faceobjects<span class="token punctuation">[</span>picked<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

      <span class="token comment">// intersection over union</span>
      <span class="token keyword keyword-float">float</span> inter_area <span class="token operator">=</span> <span class="token function">intersection_area</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword keyword-float">float</span> union_area <span class="token operator">=</span> areas<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">+</span> areas<span class="token punctuation">[</span>picked<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">-</span> inter_area<span class="token punctuation">;</span>
      <span class="token keyword keyword-float">float</span> iou <span class="token operator">=</span> inter_area <span class="token operator">/</span> union_area<span class="token punctuation">;</span>
      <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>iou <span class="token operator">&gt;</span> nms_threshold <span class="token operator">||</span> <span class="token function">isnan</span><span class="token punctuation">(</span>iou<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        keep <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token comment">// Stored for Merge</span>
        <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>iou <span class="token operator">&gt;</span> MERGE_MIN_IOU <span class="token operator">&amp;&amp;</span>
            <span class="token function">abs</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>confidence <span class="token operator">-</span> b<span class="token punctuation">.</span>confidence<span class="token punctuation">)</span> <span class="token operator">&lt;</span> MERGE_CONF_ERROR <span class="token operator">&amp;&amp;</span>
            a<span class="token punctuation">.</span>class_id <span class="token operator">==</span> b<span class="token punctuation">.</span>class_id <span class="token operator">&amp;&amp;</span> a<span class="token punctuation">.</span>class_id <span class="token operator">/</span> <span class="token number">3</span> <span class="token operator">==</span> b<span class="token punctuation">.</span>class_id <span class="token operator">/</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

<span class="token comment">//&#x6807;&#x8BB0; 3</span>
          <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            b<span class="token punctuation">.</span>points<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>points<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token comment">// cout&lt;&lt;b.pts_x.size()&lt;&lt;endl;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>keep<span class="token punctuation">)</span>
      picked<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * @brief Decode outputs.
 * @param prob Original predition output.
 * @param objects Vector of objects predicted.
 * @param img_w Width of Image.
 * @param img_h Height of Image.
 */</span>
<span class="token keyword keyword-static">static</span> <span class="token keyword keyword-void">void</span> <span class="token function">decodeOutputs</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> <span class="token keyword keyword-float">float</span> <span class="token operator">*</span>prob<span class="token punctuation">,</span> std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>Detection<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>objects<span class="token punctuation">,</span>
                          Eigen<span class="token double-colon punctuation">::</span>Matrix<span class="token operator">&lt;</span><span class="token keyword keyword-float">float</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span>transform_matrix<span class="token punctuation">,</span>
                          <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-int">int</span> img_w<span class="token punctuation">,</span> <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-int">int</span> img_h<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>Detection<span class="token operator">&gt;</span> proposals<span class="token punctuation">;</span>
  std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span><span class="token keyword keyword-int">int</span><span class="token operator">&gt;</span> strides <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">32</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>GridAndStride<span class="token operator">&gt;</span> grid_strides<span class="token punctuation">;</span>

  <span class="token function">generate_grids_and_stride</span><span class="token punctuation">(</span>INPUT_W<span class="token punctuation">,</span> INPUT_H<span class="token punctuation">,</span> strides<span class="token punctuation">,</span> grid_strides<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">generateYoloxProposals</span><span class="token punctuation">(</span>grid_strides<span class="token punctuation">,</span> prob<span class="token punctuation">,</span> transform_matrix<span class="token punctuation">,</span> BBOX_CONF_THRESH<span class="token punctuation">,</span>
                         proposals<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">qsort_descent_inplace</span><span class="token punctuation">(</span>proposals<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>proposals<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> TOPK<span class="token punctuation">)</span>
    proposals<span class="token punctuation">.</span><span class="token function">resize</span><span class="token punctuation">(</span>TOPK<span class="token punctuation">)</span><span class="token punctuation">;</span>
  std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span><span class="token keyword keyword-int">int</span><span class="token operator">&gt;</span> picked<span class="token punctuation">;</span>
  <span class="token function">nms_sorted_bboxes</span><span class="token punctuation">(</span>proposals<span class="token punctuation">,</span> picked<span class="token punctuation">,</span> NMS_THRESH<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-int">int</span> count <span class="token operator">=</span> picked<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  objects<span class="token punctuation">.</span><span class="token function">resize</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> count<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    objects<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> proposals<span class="token punctuation">[</span>picked<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token class-name">VINO_Detector</span><span class="token double-colon punctuation">::</span><span class="token function">VINO_Detector</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> std<span class="token double-colon punctuation">::</span>string <span class="token operator">&amp;</span>model_name<span class="token punctuation">,</span> <span class="token keyword keyword-bool">bool</span> debug<span class="token punctuation">)</span>

<span class="token punctuation">{</span>
  <span class="token function">initModel</span><span class="token punctuation">(</span>model_name<span class="token punctuation">)</span><span class="token punctuation">;</span>
  debug_ <span class="token operator">=</span> debug<span class="token punctuation">;</span>
  <span class="token comment">// INPUT_H = param_.input_height;</span>
  <span class="token comment">// INPUT_W = param_.input_width;</span>
  <span class="token comment">// NMS_THRESH = param_.nms_threshold;</span>
  <span class="token comment">// BBOX_CONF_THRESH = param_.conf_threshold;</span>

  class_list_<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token string">&quot;Blue_r&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  class_list_<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token string">&quot;Blue_hit&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  class_list_<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token string">&quot;Blue_over&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  class_list_<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token string">&quot;Red_r&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  class_list_<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token string">&quot;Red_hit&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  class_list_<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token string">&quot;Red_over&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  debug_img_pub_ <span class="token operator">=</span> n_<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">advertise</span><span class="token generic class-name"><span class="token operator">&lt;</span>sensor_msgs<span class="token double-colon punctuation">::</span><span class="token type-opencl-host-cpp keyword">Image</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token string">&quot;/rune/detect_result&quot;</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token class-name">VINO_Detector</span><span class="token double-colon punctuation">::</span><span class="token operator">~</span><span class="token function">VINO_Detector</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;destoryed&quot;</span> <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span> <span class="token punctuation">}</span>

<span class="token comment">// TODO:change to your dir</span>
<span class="token keyword keyword-bool">bool</span> <span class="token class-name">VINO_Detector</span><span class="token double-colon punctuation">::</span><span class="token function">initModel</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> std<span class="token double-colon punctuation">::</span>string <span class="token operator">&amp;</span>path_xml<span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token comment">// ie_.SetConfig({{CONFIG_KEY(CACHE_DIR), &quot;../.cache&quot;}});</span>
  <span class="token comment">// ie_.SetConfig({{CONFIG_KEY(GPU_THROUGHPUT_STREAMS),&quot;CPU_THROUGHPUT_AUTO&quot;}});</span>
  <span class="token comment">// ie_.SetConfig({{CONFIG_KEY(GPU_THROUGHPUT_STREAMS),&quot;1&quot;}});</span>
  <span class="token comment">// Step 1. Read a model in OpenVINO Intermediate Representation (.xml and</span>
  <span class="token comment">// .bin files) or ONNX (.onnx file) format</span>
  network_ <span class="token operator">=</span> ie_<span class="token punctuation">.</span><span class="token function">ReadNetwork</span><span class="token punctuation">(</span>path_xml<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>network_<span class="token punctuation">.</span><span class="token function">getOutputsInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-throw">throw</span> std<span class="token double-colon punctuation">::</span><span class="token function">logic_error</span><span class="token punctuation">(</span><span class="token string">&quot;Sample supports topologies with 1 output only&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// Step 2. Configure input &amp; output</span>
  <span class="token comment">//  Prepare input blobs</span>
  <span class="token comment">// InputInfo::Ptr input_info = network.getInputsInfo().begin()-&gt;second;</span>
  input_name_ <span class="token operator">=</span> network_<span class="token punctuation">.</span><span class="token function">getInputsInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span>first<span class="token punctuation">;</span>

  <span class="token comment">//  Prepare output blobs</span>
  <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>network_<span class="token punctuation">.</span><span class="token function">getOutputsInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    std<span class="token double-colon punctuation">::</span>cerr <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;Network outputs info is empty&quot;</span> <span class="token operator">&lt;&lt;</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span>
    <span class="token keyword keyword-return">return</span> EXIT_FAILURE<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token comment">// DataPtr output_info = network.getOutputsInfo().begin()-&gt;second;</span>
  output_name_ <span class="token operator">=</span> network_<span class="token punctuation">.</span><span class="token function">getOutputsInfo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span>first<span class="token punctuation">;</span>

  <span class="token comment">// output_info-&gt;setPrecision(Precision::FP16);</span>
  <span class="token comment">// Step 3. Loading a model to the device</span>
  <span class="token comment">// executable_network = ie.LoadNetwork(network, &quot;MULTI:GPU&quot;);</span>

  executable_network_ <span class="token operator">=</span> ie_<span class="token punctuation">.</span><span class="token function">LoadNetwork</span><span class="token punctuation">(</span>network_<span class="token punctuation">,</span> <span class="token string">&quot;GPU&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// executable_network = ie_.LoadNetwork(network, &quot;CPU&quot;);</span>

  <span class="token comment">// Step 4. Create an infer request</span>
  infer_request_ <span class="token operator">=</span> executable_network_<span class="token punctuation">.</span><span class="token function">CreateInferRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-const">const</span> Blob<span class="token double-colon punctuation">::</span>Ptr output_blob <span class="token operator">=</span> infer_request_<span class="token punctuation">.</span><span class="token function">GetBlob</span><span class="token punctuation">(</span>output_name_<span class="token punctuation">)</span><span class="token punctuation">;</span>
  moutput_ <span class="token operator">=</span> <span class="token generic-function"><span class="token function">as</span><span class="token generic class-name"><span class="token operator">&lt;</span>MemoryBlob<span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span>output_blob<span class="token punctuation">)</span><span class="token punctuation">;</span>
  Blob<span class="token double-colon punctuation">::</span>Ptr input <span class="token operator">=</span>
      infer_request_<span class="token punctuation">.</span><span class="token function">GetBlob</span><span class="token punctuation">(</span>input_name_<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// just wrap Mat data by Blob::Ptr</span>
  <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>moutput_<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword keyword-throw">throw</span> std<span class="token double-colon punctuation">::</span><span class="token function">logic_error</span><span class="token punctuation">(</span>
        <span class="token string">&quot;We expect output to be inherited from MemoryBlob, &quot;</span>
        <span class="token string">&quot;but by fact we were not able to cast output to MemoryBlob&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token comment">// locked memory holder should be alive all time while access to its buffer</span>
  <span class="token comment">// happens</span>

  <span class="token keyword keyword-return">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword keyword-bool">bool</span> <span class="token class-name">VINO_Detector</span><span class="token double-colon punctuation">::</span><span class="token function">detect</span><span class="token punctuation">(</span>cv<span class="token double-colon punctuation">::</span>Mat <span class="token operator">&amp;</span>src<span class="token punctuation">,</span> std<span class="token double-colon punctuation">::</span>array<span class="token operator">&lt;</span>cv<span class="token double-colon punctuation">::</span>Point2f<span class="token punctuation">,</span> <span class="token number">5</span><span class="token operator">&gt;</span> <span class="token operator">&amp;</span>result<span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>src<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    fmt<span class="token double-colon punctuation">::</span><span class="token function">print</span><span class="token punctuation">(</span>fmt<span class="token double-colon punctuation">::</span><span class="token function">fg</span><span class="token punctuation">(</span>fmt<span class="token double-colon punctuation">::</span>color<span class="token double-colon punctuation">::</span>red<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;[DETECT] ERROR: &#x4F20;&#x5165;&#x4E86;&#x7A7A;&#x7684;src\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">SAVE_AUTOAIM_LOG</span></span>
    <span class="token function">LOG</span><span class="token punctuation">(</span>ERROR<span class="token punctuation">)</span> <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;[DETECT] ERROR: &#x4F20;&#x5165;&#x4E86;&#x7A7A;&#x7684;src&quot;</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span> <span class="token comment">// SAVE_AUTOAIM_LOG</span></span>
    <span class="token keyword keyword-return">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword keyword-double">double</span> t0 <span class="token operator">=</span> ros<span class="token double-colon punctuation">::</span><span class="token class-name">Time</span><span class="token double-colon punctuation">::</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toSec</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// cv::Mat src;</span>
  <span class="token comment">// image.copyTo(src);</span>
  cv<span class="token double-colon punctuation">::</span>Mat pr_img <span class="token operator">=</span> <span class="token function">scaledResize</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> transfrom_matrix<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">SHOW_INPUT</span></span>
  <span class="token function">namedWindow</span><span class="token punctuation">(</span><span class="token string">&quot;network_input&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">imshow</span><span class="token punctuation">(</span><span class="token string">&quot;network_input&quot;</span><span class="token punctuation">,</span> pr_img<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">waitKey</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span> <span class="token comment">// SHOW_INPUT</span></span>
  cv<span class="token double-colon punctuation">::</span>Mat pre<span class="token punctuation">;</span>
  cv<span class="token double-colon punctuation">::</span>Mat pre_split<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  pr_img<span class="token punctuation">.</span><span class="token function">convertTo</span><span class="token punctuation">(</span>pre<span class="token punctuation">,</span> CV_32F<span class="token punctuation">)</span><span class="token punctuation">;</span>
  cv<span class="token double-colon punctuation">::</span><span class="token function">split</span><span class="token punctuation">(</span>pre<span class="token punctuation">,</span> pre_split<span class="token punctuation">)</span><span class="token punctuation">;</span>

  Blob<span class="token double-colon punctuation">::</span>Ptr imgBlob <span class="token operator">=</span>
      infer_request_<span class="token punctuation">.</span><span class="token function">GetBlob</span><span class="token punctuation">(</span>input_name_<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// just wrap Mat data by Blob::Ptr</span>
                                           <span class="token comment">// std::cout&lt;&lt;&quot;ok\n&quot;;</span>

  InferenceEngine<span class="token double-colon punctuation">::</span>MemoryBlob<span class="token double-colon punctuation">::</span>Ptr mblob <span class="token operator">=</span>
      InferenceEngine<span class="token double-colon punctuation">::</span><span class="token generic-function"><span class="token function">as</span><span class="token generic class-name"><span class="token operator">&lt;</span>InferenceEngine<span class="token double-colon punctuation">::</span>MemoryBlob<span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span>imgBlob<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// locked memory holder should be alive all time while access to its buffer</span>
  <span class="token comment">// happens</span>
  <span class="token keyword keyword-auto">auto</span> mblobHolder <span class="token operator">=</span> mblob<span class="token operator">-&gt;</span><span class="token function">wmap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-float">float</span> <span class="token operator">*</span>blob_data <span class="token operator">=</span> mblobHolder<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">as</span><span class="token generic class-name"><span class="token operator">&lt;</span><span class="token keyword keyword-float">float</span> <span class="token operator">*</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword keyword-auto">auto</span> img_offset <span class="token operator">=</span> INPUT_W <span class="token operator">*</span> INPUT_H<span class="token punctuation">;</span>
  <span class="token comment">// Copy img into blob</span>
  <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> c <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> c <span class="token operator">&lt;</span> <span class="token number">3</span><span class="token punctuation">;</span> c<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">memcpy</span><span class="token punctuation">(</span>blob_data<span class="token punctuation">,</span> pre_split<span class="token punctuation">[</span>c<span class="token punctuation">]</span><span class="token punctuation">.</span>data<span class="token punctuation">,</span> INPUT_W <span class="token operator">*</span> INPUT_H <span class="token operator">*</span> <span class="token keyword keyword-sizeof">sizeof</span><span class="token punctuation">(</span><span class="token keyword keyword-float">float</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    blob_data <span class="token operator">+=</span> img_offset<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token comment">// auto t1 = std::chrono::steady_clock::now();</span>
  infer_request_<span class="token punctuation">.</span><span class="token function">Infer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// auto t2 = std::chrono::steady_clock::now();</span>
  <span class="token comment">// cout&lt;&lt;(float)(std::chrono::duration&lt;double,std::milli&gt;(t2 -</span>
  <span class="token comment">// t1).count())&lt;&lt;endl; infer_request.GetPerformanceCounts();</span>
  <span class="token comment">// -----------------------------------------------------------------------------------------------------</span>
  <span class="token comment">// --------------------------- Step 8. Process output----------------</span>
  <span class="token comment">// const Blob::Ptr output_blob = infer_request.GetBlob(output_name);</span>
  <span class="token comment">// MemoryBlob::CPtr moutput = as&lt;MemoryBlob&gt;(output_blob);</span>

  <span class="token keyword keyword-auto">auto</span> moutputHolder <span class="token operator">=</span> moutput_<span class="token operator">-&gt;</span><span class="token function">rmap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-const">const</span> <span class="token keyword keyword-float">float</span> <span class="token operator">*</span>net_pred <span class="token operator">=</span>
      moutputHolder<span class="token punctuation">.</span><span class="token generic-function"><span class="token function">as</span><span class="token generic class-name"><span class="token operator">&lt;</span><span class="token keyword keyword-const">const</span> PrecisionTrait<span class="token operator">&lt;</span>Precision<span class="token double-colon punctuation">::</span>FP32<span class="token operator">&gt;</span><span class="token double-colon punctuation">::</span>value_type <span class="token operator">*</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-int">int</span> img_w <span class="token operator">=</span> src<span class="token punctuation">.</span>cols<span class="token punctuation">;</span>
  <span class="token keyword keyword-int">int</span> img_h <span class="token operator">=</span> src<span class="token punctuation">.</span>rows<span class="token punctuation">;</span>

  std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>Detection<span class="token operator">&gt;</span> objects<span class="token punctuation">;</span>
  <span class="token function">decodeOutputs</span><span class="token punctuation">(</span>net_pred<span class="token punctuation">,</span> objects<span class="token punctuation">,</span> transfrom_matrix<span class="token punctuation">,</span> img_w<span class="token punctuation">,</span> img_h<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">// for (auto object = objects.begin(); object != objects.end(); ++object)</span>
  <span class="token comment">// {</span>
  <span class="token comment">//     std::cout&lt;&lt;(*object).points.size()&lt;&lt;std::endl;</span>
  <span class="token comment">// }</span>
  <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-auto">auto</span> object <span class="token operator">=</span> objects<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> object <span class="token operator">!=</span> objects<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">++</span>object<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">//&#x5BF9;&#x5019;&#x9009;&#x6846;&#x9884;&#x6D4B;&#x89D2;&#x70B9;&#x8FDB;&#x884C;&#x5E73;&#x5747;,&#x964D;&#x4F4E;&#x8BEF;&#x5DEE;</span>

<span class="token comment">//&#x6807;&#x8BB0; 4</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token operator">*</span>object<span class="token punctuation">)</span><span class="token punctuation">.</span>points<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;=</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword keyword-auto">auto</span> N <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator">*</span>object<span class="token punctuation">)</span><span class="token punctuation">.</span>points<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      cv<span class="token double-colon punctuation">::</span>Point2f pts_final<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

      <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> N<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        pts_final<span class="token punctuation">[</span>i <span class="token operator">%</span> <span class="token number">5</span><span class="token punctuation">]</span> <span class="token operator">+=</span> <span class="token punctuation">(</span><span class="token operator">*</span>object<span class="token punctuation">)</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>

      <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        pts_final<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>x <span class="token operator">=</span> pts_final<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>x <span class="token operator">/</span> <span class="token punctuation">(</span>N <span class="token operator">*</span> <span class="token number">0.2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        pts_final<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>y <span class="token operator">=</span> pts_final<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>y <span class="token operator">/</span> <span class="token punctuation">(</span>N <span class="token operator">*</span> <span class="token number">0.2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>

      <span class="token punctuation">(</span><span class="token operator">*</span>object<span class="token punctuation">)</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> pts_final<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
      <span class="token punctuation">(</span><span class="token operator">*</span>object<span class="token punctuation">)</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> pts_final<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
      <span class="token punctuation">(</span><span class="token operator">*</span>object<span class="token punctuation">)</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> pts_final<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
      <span class="token punctuation">(</span><span class="token operator">*</span>object<span class="token punctuation">)</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> pts_final<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
      <span class="token punctuation">(</span><span class="token operator">*</span>object<span class="token punctuation">)</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span> <span class="token operator">=</span> pts_final<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// (*object).area = (int)(calcTetragonArea((*object).apex));</span>
  <span class="token punctuation">}</span>

  <span class="token keyword keyword-double">double</span> t1 <span class="token operator">=</span> ros<span class="token double-colon punctuation">::</span><span class="token class-name">Time</span><span class="token double-colon punctuation">::</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toSec</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>debug_<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">debugProcess</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> objects<span class="token punctuation">,</span> <span class="token punctuation">(</span>t1 <span class="token operator">-</span> t0<span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token number">1000.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>objects<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-return">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>

  <span class="token comment">// get 5 points</span>
  <span class="token comment">//--------------------------------------------------------------------------------------------------------------</span>

<span class="token comment">//&#x6807;&#x8BB0; 5</span>

  std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>cv<span class="token double-colon punctuation">::</span>Point<span class="token operator">&gt;</span> final_r<span class="token punctuation">,</span> final_hit<span class="token punctuation">;</span>
  <span class="token keyword keyword-float">float</span> max_r_conf <span class="token operator">=</span> <span class="token number">0.0</span><span class="token punctuation">,</span> max_hit_conf <span class="token operator">=</span> <span class="token number">0.0</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-int">int</span> max_r_idx <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> max_hit_idx <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> objects<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// find r</span>
    <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>objects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>class_id <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> objects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>class_id <span class="token operator">==</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>objects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>confidence <span class="token operator">&gt;</span> max_r_conf<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        max_r_idx <span class="token operator">=</span> i<span class="token punctuation">;</span>
        max_r_conf <span class="token operator">=</span> objects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>confidence<span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// find hit</span>
    <span class="token keyword keyword-else">else</span> <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>objects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>class_id <span class="token operator">==</span> <span class="token number">1</span> <span class="token operator">||</span> objects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>class_id <span class="token operator">==</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>objects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>confidence <span class="token operator">&gt;</span> max_hit_conf<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        max_hit_idx <span class="token operator">=</span> i<span class="token punctuation">;</span>
        max_hit_conf <span class="token operator">=</span> objects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>confidence<span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token keyword keyword-if">if</span> <span class="token punctuation">(</span>max_r_idx <span class="token operator">&lt;</span> <span class="token number">0</span> <span class="token operator">||</span> max_hit_idx <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token keyword keyword-return">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>

  cv<span class="token double-colon punctuation">::</span>Point r_pt<span class="token punctuation">;</span>
  r_pt<span class="token punctuation">.</span>x <span class="token operator">=</span> <span class="token punctuation">(</span>objects<span class="token punctuation">[</span>max_r_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>x <span class="token operator">+</span> objects<span class="token punctuation">[</span>max_r_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>x <span class="token operator">+</span>
            objects<span class="token punctuation">[</span>max_r_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span>x <span class="token operator">+</span> objects<span class="token punctuation">[</span>max_r_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span>x <span class="token operator">+</span>
            objects<span class="token punctuation">[</span>max_r_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">.</span>x<span class="token punctuation">)</span> <span class="token operator">*</span>
           <span class="token number">0.2</span><span class="token punctuation">;</span>
  r_pt<span class="token punctuation">.</span>y <span class="token operator">=</span> <span class="token punctuation">(</span>objects<span class="token punctuation">[</span>max_r_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>y <span class="token operator">+</span> objects<span class="token punctuation">[</span>max_r_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>y <span class="token operator">+</span>
            objects<span class="token punctuation">[</span>max_r_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span>y <span class="token operator">+</span> objects<span class="token punctuation">[</span>max_r_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span>y <span class="token operator">+</span>
            objects<span class="token punctuation">[</span>max_r_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">.</span>y<span class="token punctuation">)</span> <span class="token operator">*</span>
           <span class="token number">0.2</span><span class="token punctuation">;</span>

  <span class="token comment">// save result</span>

  result<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> r_pt<span class="token punctuation">;</span>
  result<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> objects<span class="token punctuation">[</span>max_hit_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  result<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> objects<span class="token punctuation">[</span>max_hit_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  result<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> objects<span class="token punctuation">[</span>max_hit_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  result<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span> <span class="token operator">=</span> objects<span class="token punctuation">[</span>max_hit_idx<span class="token punctuation">]</span><span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token keyword keyword-return">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword keyword-void">void</span> <span class="token class-name">VINO_Detector</span><span class="token double-colon punctuation">::</span><span class="token function">debugProcess</span><span class="token punctuation">(</span><span class="token keyword keyword-const">const</span> cv<span class="token double-colon punctuation">::</span>Mat <span class="token operator">&amp;</span>src<span class="token punctuation">,</span>
                                 <span class="token keyword keyword-const">const</span> std<span class="token double-colon punctuation">::</span>vector<span class="token operator">&lt;</span>Detection<span class="token operator">&gt;</span> <span class="token operator">&amp;</span>objects<span class="token punctuation">,</span>
                                 <span class="token keyword keyword-double">double</span> t<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  cv<span class="token double-colon punctuation">::</span>Mat debug_img <span class="token operator">=</span> src<span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// ROS_INFO(&quot;%d&quot;,objects.size());</span>
  cv<span class="token double-colon punctuation">::</span><span class="token function">putText</span><span class="token punctuation">(</span>debug_img<span class="token punctuation">,</span> <span class="token string">&quot;interface time: &quot;</span> <span class="token operator">+</span> std<span class="token double-colon punctuation">::</span><span class="token function">to_string</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot; ms&quot;</span><span class="token punctuation">,</span>
              cv<span class="token double-colon punctuation">::</span><span class="token function">Point</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">,</span> cv<span class="token double-colon punctuation">::</span>FONT_HERSHEY_SIMPLEX<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span>
              cv<span class="token double-colon punctuation">::</span><span class="token function">Scalar</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> objects<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword keyword-auto">auto</span> detection <span class="token operator">=</span> objects<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-auto">auto</span> box <span class="token operator">=</span> detection<span class="token punctuation">.</span>box<span class="token punctuation">;</span>
    <span class="token keyword keyword-auto">auto</span> classId <span class="token operator">=</span> detection<span class="token punctuation">.</span>class_id<span class="token punctuation">;</span>
    <span class="token keyword keyword-auto">auto</span> color <span class="token operator">=</span> colors<span class="token punctuation">[</span>classId <span class="token operator">%</span> colors<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    cv<span class="token double-colon punctuation">::</span><span class="token function">putText</span><span class="token punctuation">(</span>debug_img<span class="token punctuation">,</span> class_list_<span class="token punctuation">[</span>classId<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                cv<span class="token double-colon punctuation">::</span><span class="token function">Point</span><span class="token punctuation">(</span>box<span class="token punctuation">.</span>x<span class="token punctuation">,</span> box<span class="token punctuation">.</span>y <span class="token operator">-</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span> cv<span class="token double-colon punctuation">::</span>FONT_HERSHEY_SIMPLEX<span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">,</span>
                cv<span class="token double-colon punctuation">::</span><span class="token function">Scalar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    cv<span class="token double-colon punctuation">::</span><span class="token function">putText</span><span class="token punctuation">(</span>debug_img<span class="token punctuation">,</span> std<span class="token double-colon punctuation">::</span><span class="token function">to_string</span><span class="token punctuation">(</span>detection<span class="token punctuation">.</span>confidence<span class="token punctuation">)</span><span class="token punctuation">,</span>
                cv<span class="token double-colon punctuation">::</span><span class="token function">Point</span><span class="token punctuation">(</span>box<span class="token punctuation">.</span>x <span class="token operator">+</span> box<span class="token punctuation">.</span>width <span class="token operator">*</span> <span class="token number">0.5</span><span class="token punctuation">,</span> box<span class="token punctuation">.</span>y <span class="token operator">-</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                cv<span class="token double-colon punctuation">::</span>FONT_HERSHEY_SIMPLEX<span class="token punctuation">,</span> <span class="token number">0.5</span><span class="token punctuation">,</span> cv<span class="token double-colon punctuation">::</span><span class="token function">Scalar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword keyword-for">for</span> <span class="token punctuation">(</span><span class="token keyword keyword-int">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      cv<span class="token double-colon punctuation">::</span><span class="token function">putText</span><span class="token punctuation">(</span>debug_img<span class="token punctuation">,</span> std<span class="token double-colon punctuation">::</span><span class="token function">to_string</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span><span class="token punctuation">,</span> detection<span class="token punctuation">.</span>points<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span>
                  cv<span class="token double-colon punctuation">::</span>FONT_HERSHEY_SIMPLEX<span class="token punctuation">,</span> <span class="token number">0.75</span><span class="token punctuation">,</span> cv<span class="token double-colon punctuation">::</span><span class="token function">Scalar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      cv<span class="token double-colon punctuation">::</span><span class="token function">line</span><span class="token punctuation">(</span>debug_img<span class="token punctuation">,</span> detection<span class="token punctuation">.</span>points<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span> detection<span class="token punctuation">.</span>points<span class="token punctuation">[</span><span class="token punctuation">(</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">%</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
               color<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  std_msgs<span class="token double-colon punctuation">::</span>Header hd<span class="token punctuation">;</span>
  hd<span class="token punctuation">.</span>stamp <span class="token operator">=</span> ros<span class="token double-colon punctuation">::</span><span class="token class-name">Time</span><span class="token double-colon punctuation">::</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  hd<span class="token punctuation">.</span>frame_id <span class="token operator">=</span> <span class="token string">&quot;detector&quot;</span><span class="token punctuation">;</span>
  debug_img_pub_<span class="token punctuation">.</span><span class="token function">publish</span><span class="token punctuation">(</span>
      cv_bridge<span class="token double-colon punctuation">::</span><span class="token function">CvImage</span><span class="token punctuation">(</span>hd<span class="token punctuation">,</span> <span class="token string">&quot;bgr8&quot;</span><span class="token punctuation">,</span> debug_img<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toImageMsg</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token punctuation">}</span> <span class="token comment">// namespace rune_detector</span>
</pre><ul>
<li>&#x6807;&#x8BB0; 1 &#x5230;&#x6807;&#x8BB0; 4 &#x5904;&#x662F;&#x9700;&#x8981;&#x6839;&#x636E;&#x5173;&#x952E;&#x70B9;&#x6570;&#x91CF;&#x6765;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#x7684;&#x3002;&#x56E0;&#x4E3A;&#x6BD4;&#x8F83;&#x590D;&#x6742;&#xFF0C;&#x4E0D;&#x592A;&#x597D;&#x7528;&#x6587;&#x5B57;&#x63CF;&#x8FF0;&#xFF0C;&#x4E4B;&#x540E;&#x5C3D;&#x91CF;&#x53E3;&#x5934;&#x8BB2;&#x89E3;&#x4E00;&#x4E0B;&#x5427;&#x3002;</li>
</ul>
<p>&#x8C03;&#x7528;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre data-role="codeBlock" data-info="cpp" class="language-cpp"><span class="token comment">//&#x52A0;&#x8F7D;&#x6A21;&#x578B;</span>
VINO_Detector<span class="token double-colon punctuation">::</span>Ptr VINOdetector <span class="token operator">=</span> std<span class="token double-colon punctuation">::</span><span class="token generic-function"><span class="token function">make_unique</span><span class="token generic class-name"><span class="token operator">&lt;</span>VINO_Detector<span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span>model_path<span class="token punctuation">,</span> is_debug_<span class="token punctuation">)</span><span class="token punctuation">;</span>

std<span class="token double-colon punctuation">::</span>array<span class="token operator">&lt;</span>cv<span class="token double-colon punctuation">::</span>Point<span class="token punctuation">,</span><span class="token number">5</span><span class="token operator">&gt;</span> result<span class="token punctuation">;</span>
VINOdetector<span class="token operator">-&gt;</span><span class="token function">detect</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//detect &#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x4E0E; yolo-face &#x4E00;&#x6837;</span>
</pre><ul>
<li>&#x5982;&#x679C;&#x8981;&#x7528;&#x5728;&#x5176;&#x5B83;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x9700;&#x8981;&#x81EA;&#x884C;&#x4FEE;&#x6539; detect &#x51FD;&#x6570;&#x7684;&#x5165;&#x53E3;&#x53C2;&#x6570;&#x548C;&#x51FD;&#x6570;&#x5185;&#x5BB9;&#x3002;&#x53EF;&#x4EE5;&#x5220;&#x9664;&#x6807;&#x8BB0; 5 &#x540E;&#x9762;&#x7684;&#x5185;&#x5BB9;&#x5E76;&#x4FEE;&#x6539;&#x5165;&#x53E3;&#x51FD;&#x6570;&#xFF0C;&#x4F7F;&#x5176;&#x4F7F;&#x7528;&#x8303;&#x56F4;&#x66F4;&#x5E7F;&#x3002;</li>
</ul>

      </div>
      
      
    
    
    
    
    
    
    
    
  
    </body></html>